package com.bawei.module_goods.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.mvi.base.BaseViewModel
import com.bawei.module_goods.intent.GoodsIntent
import com.bawei.module_goods.repository.GoodsRepository
import com.bawei.module_goods.state.GoodsState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch


class GoodsViewModel : BaseViewModel<GoodsIntent>() {
    private val repo: GoodsRepository by lazy { GoodsRepository() }
    private var _mallHomeStateFlow = MutableStateFlow<GoodsState>(GoodsState.Init)
    val mallHomeStateFlow = _mallHomeStateFlow.asStateFlow()

    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect { intent ->
                when (intent) {
                    is GoodsIntent.LoadHomeData -> {
                        loadHomeData()
                    }
                }
            }
        }
    }

    fun loadHomeData() {
        viewModelScope.launch {
            // Banner
            repo.getBannerList()
                .catch {
                    _mallHomeStateFlow.value = GoodsState.onFailed(it.message.toString())
                }.filter { it != null }
                .collect {
                    if (it.code == 200 && it.data != null) {
                        _mallHomeStateFlow.value = GoodsState.onSuccess(it.data!!, "banner")
                    } else {
                        _mallHomeStateFlow.value = GoodsState.onFailed(it.msg)
                    }
                }
            // Group
            repo.getGroupList()
                .catch {
                    _mallHomeStateFlow.value = GoodsState.onFailed(it.message.toString())
                }.filter { it != null }
                .collect {
                    if (it.code == 200 && it.data != null) {
                        _mallHomeStateFlow.value = GoodsState.onSuccess(it.data!!, "group")
                    } else {
                        _mallHomeStateFlow.value = GoodsState.onFailed(it.msg)
                    }
                }
            // Event
            repo.getEventList()
                .catch {
                    _mallHomeStateFlow.value = GoodsState.onFailed(it.message.toString())
                }.filter { it != null }
                .collect {
                    if (it.code == 200 && it.data != null) {
                        _mallHomeStateFlow.value = GoodsState.onSuccess(it.data!!, "event")
                    } else {
                        _mallHomeStateFlow.value = GoodsState.onFailed(it.msg)
                    }
                }
            // Goods
            repo.getGoodsList()
                .catch {
                    _mallHomeStateFlow.value = GoodsState.onFailed(it.message.toString())
                }.filter { it != null }
                .collect {
                    if (it.code == 200 && it.data != null) {
                        _mallHomeStateFlow.value = GoodsState.onSuccess(it.data!!, "goods")
                    } else {
                        _mallHomeStateFlow.value = GoodsState.onFailed(it.msg)
                    }
                }
        }
    }
} 